package com.dw.util;

import com.dw.model.invoice.Invoice;
import com.itextpdf.text.Document;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Font;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.File;
import java.io.FileOutputStream;

public class PDFUtil {

    // 使用iText生成PDF文件
    public static String generateInvoicePDF(Invoice invoice, String remarks) throws Exception {
        String fileName = "invoice_" + invoice.getInvoiceNo() + ".pdf";
        String savePath = System.getProperty("user.home") + "/Downloads/" + fileName;

        // 创建PDF文档
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(savePath));

        document.open();

        // 指定支持中文的字体路径（假设您有 SimSun.ttf 字体）
        String fontPath = "C:\\Windows\\Fonts\\simsun.ttc";  // 这是一个 Windows 系统中的字体路径
        BaseFont baseFont = BaseFont.createFont(fontPath + ",0", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        Font font = new Font(baseFont, 12);

        // 向PDF中写入发票信息，使用指定的字体
        document.add(new Paragraph("发票号: " + invoice.getInvoiceNo(), font));
        document.add(new Paragraph("患者ID: " + invoice.getPatientId(), font));
        document.add(new Paragraph("医生ID: " + invoice.getDoctorId(), font));
        document.add(new Paragraph("金额: " + invoice.getAmount() + " 元", font));
        document.add(new Paragraph("备注: " + remarks, font));

        document.close();

        return savePath;
    }
}